﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class FormParseRequestParamWorker
	{
		public static void  parseRequestParam(Form form)
		{
			ArrayList piFieldList = new ArrayList();
			ArrayList fieldList = new ArrayList();
			parseFieldLists( form.Manifest, form.ParamTable, piFieldList, fieldList );
			
			form.PiFieldList = piFieldList;
			form.FieldList = fieldList;
		}

		//CID 0352 //2009.07.05 //Begin
		public static void parseFieldLists( Manifest manifest, Hashtable paramTable, ArrayList piFieldList, ArrayList fieldList )
		{
			foreach( string key in paramTable.Keys )
			{
				string value_Renamed = (string) paramTable[key];
				if (key.StartsWith(JetConstant.DocId)) 
				{
					if (key.StartsWith(JetConstant.PIPREFIX)) 
					{
						//CID 0331 //2009.07.27 //Begin
						value_Renamed = StringUtil.formatXmlField( value_Renamed );
						//CID 0331 //2009.07.27 //End
						string fieldName = key.Substring(JetConstant.PIPREFIX.Length);
						Field field = new Field(fieldName, value_Renamed);
						piFieldList.Add(field);
					}
					else 
					{
                        //CID 1022 //2011.10.17 //Begin
						String fieldKey = key.Substring(JetConstant.DocId.Length);
						Field field = new Field(fieldKey, value_Renamed);
                        if (!parseIndicesFieldName(field)) 
						{
							throw new InfoJetException("invalid xml field", "FPRP04");
						}
						//CID 0331 //2009.07.27 //Begin
						//总体思路是,除了Xhtml文档,Xhtml内容和PlainText,其它情况都进行格式化.
						//如果是一个Xhtml元素.
						if( SchemaTypeWorker.isXhtmlElementByFullName( manifest, field.Name ) )
						{
							try
							{
								//判断是否是一个XHTML Document.
								if( !SchemaUtil.isXhtmlDocument( field.Value ) )
								{
									//否则进行格式化.
                                    //CID 1022 //2011.10.17 //Begin
                                    if (!manifest.FormFieldsCacheEnabled)
                                    {
                                        field.Value = StringUtil.formatXmlField(field.Value);
                                    }
                                    //CID 1022 //2011.10.17 //End
                                }
                                else
                                {
                                    //CID 0664 //2010.11.24 //Begin
                                    field.IsRichText = true;
                                    //CID 0664 //2010.11.24 //End
                                }
							}
							catch( Exception xe )
							{
								//如果不是一个XmlDocument.
								try
								{
									//测试能否放到一个Xml元素里.
									//如果不是Xhtml内容,则进行格式化.
                                    if (!SchemaUtil.isXhtmlContent(field.Value))
                                    {
                                        //CID 1022 //2011.10.17 //Begin
                                        if (!manifest.FormFieldsCacheEnabled)
                                        {
                                            field.Value = StringUtil.formatXmlField(field.Value);
                                        }
                                        //CID 1022 //2011.10.17 //End
                                    }
                                    //CID 1338 //2012.09.18 //Begin
                                    //如果是XHTML内容或是PlainText,不作修改.
                                    else
                                    {
                                        //CID 0664 //2010.11.24 //Begin
                                        field.IsRichText = true;
                                        //CID 0664 //2010.11.24 //End
                                    }
                                    //CID 1338 //2012.09.18 //End
								}
								catch( Exception teste )
								{
                                    //CID 1022 //2011.10.17 //Begin
                                    if (!manifest.FormFieldsCacheEnabled)
                                    {
                                        //如果不能放到Xml元素中,进行格式化.
                                        field.Value = StringUtil.formatXmlField(field.Value);
                                    }
                                    //CID 1022 //2011.10.17 //End
								}
							}
						}
						else
						{
                            if (!manifest.FormFieldsCacheEnabled)
                            {
                                field.Value = StringUtil.formatXmlField(field.Value);
                            }
                            //CID 1022 //2011.10.17 //End
						}
						//CID 0331 //2009.07.27 //End
						fieldList.Add(field);
					}
				}
			}
		}
		//CID 0352 //2009.07.05 //End
		
		private static bool parseIndicesFieldName(Field field)
		{
			string fieldName = field.Name;
			int nameStart = findNameStart(fieldName);
			string coords = fieldName.Substring(0, nameStart);
			int[] intCoords = parseCoords(coords);
			if (intCoords == null)
			{
				return false;
			}
			field.Coords = intCoords;
			string name = fieldName.Substring(nameStart);
			field.Name = name;
			return true;
		}
		
		private static int[] parseCoords(string coords)
		{
			StringTokenizer coordsTokenizer = new StringTokenizer(coords, "_");
			int[] intCoords = new int[coordsTokenizer.Count];
			int i = 0;
			while (coordsTokenizer.HasMoreTokens())
			{
				string coord = coordsTokenizer.NextToken();
				try
				{
					intCoords[i] = System.Int32.Parse(coord);
					i++;
				}
				catch (Exception e)
				{
					return null;
				}
			}
			return intCoords;
		}
		
		private static int findNameStart(string str)
		{
			char chr1 = (char) (0);
			char chr2 = (char) (0);
			int length = str.Length;
			for (int i = 0; i < length; i++)
			{
				if (i < length - 1)
				{
					chr1 = str[i];
					chr2 = str[i + 1];
					if ((chr1 == '_') && (!System.Char.IsDigit(chr2)))
					{
						return i + 1;
					}
				}
			}
			return - 1;
		}
	}
}